package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.CollectiveframEntity;
import com.shaoshaohuo.app.entity.FarmMyShop;
import com.shaoshaohuo.app.net.HttpConfig;
import com.shaoshaohuo.app.net.NewHttpCofig;
import com.shaoshaohuo.app.net.RequestParam;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.utils.sharesdk.ShareContent;
import com.shaoshaohuo.app.view.SelfadaptionImageview;
import com.shaoshaohuo.app.view.ShareDialog;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/9.
 */

public class CollectivefarmDelatisActivity extends BaseActivity implements View.OnClickListener {
    private ImageView fragment_detatop_vp;
    private TextView fragment_detatop_tv_nmae;
    private TextView fragment_detatop_tv_detalis;
    private TextView pick_fivestart;
    private TextView pick_tv_yuanjia;
    private TextView piuck_wifi;
    private TextView pick_tv_gongxiangjia;
    private TextView pick_tv_ertongjia;
    private LinearLayout gm_imalist;
    private TextView tv_fabutime;
    private TextView tv_ck;
    private TextView tv_sc;
    private TextView tv_remark;
    private TextView tv_shoper;
    private FarmMyShop.DataBean data1;
    private Handler handler = new Handler();
    private Button fragment_detalis_top_btn_gm;
    private String imaurl;
    private String catid;

    private ShopCallBack shopCallBack;

    public void setShopCallBack(ShopCallBack shopCallBack) {
        this.shopCallBack = shopCallBack;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collectivefarm);
        Intent intent = getIntent();
        data1 = (FarmMyShop.DataBean) intent.getSerializableExtra("data");
        catid = intent.getStringExtra("catid");
        imaurl = intent.getStringExtra("imgurl");
        ViewGroup content = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
        View childAt = content.getChildAt(0);
        ImageView detalisActivity_share = (ImageView) findViewById(R.id.detalisActivity_share);
        detalisActivity_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareDialog shareDialog = new ShareDialog(v.getContext());
                Platform.ShareParams shareParams = new Platform.ShareParams();
                shareParams.setTitle("妈妈爱果");
                shareParams.setTitleUrl(HttpConfig.BASE_HOST_URL + NewHttpCofig.Share + "?catid=" + data1.getCatid());
                shareParams.setText(ShareContent.SHARE_APP_CONTENT);
                shareParams.setImageUrl(imaurl);
                shareParams.setUrl(HttpConfig.BASE_HOST_URL + NewHttpCofig.Share + "?catid=" + catid);
                shareDialog.setshareParams(shareParams);
                shareDialog.show();
            }
        });
        changeTransltestaus(this, childAt, TransltestausModel.NoePicter, Color.parseColor("#B2000000"));
        ImageView detalisActivity_back = (ImageView) findViewById(R.id.detalisActivity_back);
        detalisActivity_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        init();
        setView();
        network();
    }

    private void setView() {
        Picasso.with(this).load(data1.getImg()).placeholder(R.mipmap.zhanzhanzhan).into(fragment_detatop_vp);
        fragment_detatop_tv_nmae.setText(data1.getProName());
        tv_fabutime.setText("发布时间：" + data1.getPushTime());
        tv_ck.setText("已查看:" + data1.getScanNum() + "次");
        tv_sc.setText("已收藏:" + data1.getCollection() + "");
        tv_remark.setText("已评价:" + data1.getRemarkNum() + "");
        tv_shoper.setText("已购买:" + data1.getPayNum() + "");
    }

    private void network() {
        ShopHttpConfig.Collectiveframqurey(this, data1.getCatid(), new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                final CollectiveframEntity collectiveframEntity = (CollectiveframEntity) o;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        CollectiveframEntity.DataBean data = collectiveframEntity.getData();
                        fragment_detatop_tv_detalis.setText(data.getAddress());
                        if (data.getWifi().equals("1")) {
                            piuck_wifi.setText("(提供wifi)");
                        } else {
                            piuck_wifi.setText("(无wifi)");
                        }

                        pick_tv_ertongjia.setText(data.getChildPrice());
                        pick_tv_gongxiangjia.setText(data.getTeamPrice());
                        pick_tv_yuanjia.setText(data.getAdultPrice());
                        String staTime = data.getStaTime();
                        String endTime = data.getEndTime();
                        pick_fivestart.setText("供货时间：" + staTime + "--" + endTime);
                        List<CollectiveframEntity.DataBean.ImgMapBean> imgMap1 = data.getImgMap();
                        for (int i = 0; i < imgMap1.size(); i++) {
                            SelfadaptionImageview imageView = new SelfadaptionImageview(CollectivefarmDelatisActivity.this);
                            gm_imalist.addView(imageView);
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                            Picasso.with(CollectivefarmDelatisActivity.this).load(imgMap1.get(i).getImg()).placeholder(R.mipmap.zhanzhanzhan).into(imageView);
                        }
                    }
                });
            }
        }, CollectiveframEntity.class);
    }

    private void init() {
        fragment_detatop_vp = (ImageView) findViewById(R.id.fragment_detatop_vp);
        fragment_detatop_tv_nmae = (TextView) findViewById(R.id.fragment_detatop_tv_nmae);
        fragment_detatop_tv_detalis = (TextView) findViewById(R.id.fragment_detatop_tv_detalis);
        pick_fivestart = (TextView) findViewById(R.id.pick_fivestart);
        piuck_wifi = (TextView) findViewById(R.id.piuck_wifi);
        pick_tv_yuanjia = (TextView) findViewById(R.id.pick_tv_yuanjia);
        pick_tv_gongxiangjia = (TextView) findViewById(R.id.pick_tv_gongxiangjia);
        pick_tv_ertongjia = (TextView) findViewById(R.id.pick_tv_ertongjia);
        gm_imalist = (LinearLayout) findViewById(R.id.gm_imalist);
        tv_fabutime = (TextView) findViewById(R.id.tv_fabutime);
        tv_ck = (TextView) findViewById(R.id.tv_ck);
        tv_sc = (TextView) findViewById(R.id.tv_sc);
        tv_remark = (TextView) findViewById(R.id.tv_remark);
        tv_shoper = (TextView) findViewById(R.id.tv_shoper);
        Button pick_zongjia = (Button) findViewById(R.id.pick_zongjia);
        pick_zongjia.setOnClickListener(this);
        fragment_detalis_top_btn_gm = (Button) findViewById(R.id.fragment_detalis_top_btn_gm);
        fragment_detalis_top_btn_gm.setOnClickListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) findViewById(R.id.detalisActivity_relatopbar).getLayoutParams();
            layoutParams.setMargins(0, getStatusBarHeight(this), 0, 0);
        }
    }

    @Override
    public void onClick(View v) {
        Map<String, String> headerParams = RequestParam.getHeaderParams();
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        switch (v.getId()) {
            case R.id.pick_zongjia:
                //：/shoper/manager
                stringStringHashMap.clear();
                stringStringHashMap.put("catid", data1.getCatid());
                stringStringHashMap.put("type", "2");
                Okhttputils.Instanse(this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + "/shoper/manager", stringStringHashMap, new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("刪除失敗");
                            }
                        });
                    }

                    @Override
                    public void error(Call call, int code) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("刪除失敗");
                            }
                        });
                    }

                    @Override
                    public void suseff(Call call, Response response, Object o) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent();
                                intent.setAction("farmdata");
                                sendBroadcast(intent);
                                finish();
                            }
                        });
                    }
                }, BaseEntity.class);
                break;
            case R.id.fragment_detalis_top_btn_gm:
                stringStringHashMap.clear();
                stringStringHashMap.put("catid", data1.getCatid());
                stringStringHashMap.put("type", "1");
                Okhttputils.Instanse(this).postFromBody(headerParams, HttpConfig.BASE_HOST_URL + "/shoper/manager", stringStringHashMap, new Okhttputils.OkutListener() {
                    @Override
                    public void error(Call call, IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("刪除失敗");
                            }
                        });
                    }

                    @Override
                    public void error(Call call, int code) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.toast("刪除失敗");
                            }
                        });
                    }

                    @Override
                    public void suseff(Call call, Response response, final Object o) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                CollectiveDelatisEntity collectiveDelatisEntity = (CollectiveDelatisEntity) o;
                                String status = collectiveDelatisEntity.getData().getStatus();
                                //  3上架  2下架
                                if (status.equals("3")) {
                                    fragment_detalis_top_btn_gm.setText("商品上架");
                                    ToastUtil.toast("上架成功");
                                    Intent intent = new Intent();
                                    intent.setAction("farmdata");
                                    sendBroadcast(intent);
                                } else if (status.equals("2")) {
                                    fragment_detalis_top_btn_gm.setText("商品下架");
                                    ToastUtil.toast("下架成功");
                                    Intent intent = new Intent();
                                    intent.setAction("farmdata");
                                    sendBroadcast(intent);
                                }

                            }
                        });
                    }
                }, CollectiveDelatisEntity.class);
                break;
        }
    }

    public interface ShopCallBack {
        void callBackListener(String type);
    }
}
